class swinUI extends egret.Sprite {

    public constructor() {
        super();
        this.createView();
    }

    private textField: egret.TextField;
    private swin_shuihuaBG3;
    private swin_shuihuaBG;
    private swin_shuihuaBG2;
    private swin_shuihuaBG2_1;
    private zhongdian;
    private speed = 5;
    private mc;
    private infoText;
    private rankinglist;
    private jishi_time;
    private fps = 16;
    private mcFactory;
    private swin_huanhu;//欢呼声音
    private swin_sound;//游泳声音
    private soundChannel;
    private count1;//倒数32
    private count2;//倒数1
    private createView(): void {
        //游泳欢呼
        this.swin_huanhu = new egret.Sound();
        this.swin_huanhu.load("resource/assets/swin_huanhu.mp3");

        //倒数32音效
        this.count1 = new egret.Sound;
        this.count1.load("resource/assets/count1.mp3");

        //倒数1音效
        this.count2 = new egret.Sound();
        this.count2.load("resource/assets/count2.mp3");


        //游戏背景
        var swin_bg = createBitmap("swin_bg_png")
        this.addChild(swin_bg);
        //移动水花背景
        this.swin_shuihuaBG = div(0, 0, 640, 0, 640, 1136, 0, 1136, 0, 0, 0, 0);
        this.addChild(this.swin_shuihuaBG);
        this.swin_shuihuaBG2 = div(0, 0, 640, 0, 640, 1136, 0, 1136, 0, 0, 0, -1136);
        this.addChild(this.swin_shuihuaBG2);
        this.swin_shuihuaBG2_1 = div(0, 0, 640, 0, 640, 1136, 0, 1136, 0, 0, 0, -2272);
        this.addChild(this.swin_shuihuaBG2_1);
        this.swin_shuihuaBG3 = div(0, 0, 640, 0, 640, 1136, 0, 1136, 0, 0, 0, -3408);
        this.addChild(this.swin_shuihuaBG3);
        // this.swin_shuihuaBG2.
        var swin_bowen = createBitmap("swin_shuihua_png", 0, 0);
        var swin_bowen2 = createBitmap("swin_shuihua_png", 0, 0);
        var swin_bowen3 = createBitmap("swin_shuihua_png", 0, 0);
        var swin_bowen4 = createBitmap("swin_shuihua_png", 0, 0);

        this.swin_shuihuaBG.addChild(swin_bowen)
        this.swin_shuihuaBG2.addChild(swin_bowen2)
        this.swin_shuihuaBG2_1.addChild(swin_bowen4)
        this.swin_shuihuaBG3.addChild(swin_bowen3)
        var swin_lianbian = createBitmap("swin_2bian_png", 96, 0);
        var swin_lianbian2 = createBitmap("swin_2bian_png", 96, 0);
        var swin_lianbian3 = createBitmap("swin_2bian_png", 96, 0);
        var swin_lianbian4 = createBitmap("swin_2bian_png", 96, 0);
        this.swin_shuihuaBG.addChild(swin_lianbian)
        this.swin_shuihuaBG2.addChild(swin_lianbian2)
        this.swin_shuihuaBG2_1.addChild(swin_lianbian4)
        this.swin_shuihuaBG3.addChild(swin_lianbian3)
        //游泳底层线
        var swin_dixian = createBitmap("swin_dixian_png", 278, 0)
        var swin_dixian2 = createBitmap("swin_dixian3_03_png", 305, 0)
        var swin_dixian3 = createBitmap("swin_dianx2_png", 278, 0)
        var swin_dixian4 = createBitmap("swin_dixian3_03_png", 305, 0)
        this.swin_shuihuaBG.addChild(swin_dixian);
        this.swin_shuihuaBG2.addChild(swin_dixian2);
        this.swin_shuihuaBG2_1.addChild(swin_dixian4)
        this.swin_shuihuaBG3.addChild(swin_dixian3);

        //终点
        this.zhongdian = div(0, 0, 640, 0, 640, 310, 310, 0, 0, 0, 0, -310)
        var zhongdian_1 = createBitmap("swin_zhongdian_png");
        this.zhongdian.addChild(zhongdian_1);
        this.addChild(this.zhongdian);
        //左右提示箭头
        var swin_leftjian = createBitmap("swin_jiantou_02_png", 58, 922);
        this.addChild(swin_leftjian);
        var swin_rightjian = createBitmap("swin_jiantou_03_png", 320, 922);
        this.addChild(swin_rightjian);
        //头像
        var swin_touxiang = createBitmap("swin_touxiang_png", 42, 34);
        this.addChild(swin_touxiang);
        //测试碰撞
        this.infoText = createTextFiled("WIN！")
        this.infoText.textColor = 0xfff434;
        this.infoText.multiline = true;
        this.infoText.size = 174;
        this.infoText.anchorOffsetX = this.infoText.width * .5;
        this.infoText.anchorOffsetY = this.infoText.height * .5;
        this.infoText.scaleX = 0
        this.infoText.scaleY = 0
        // this.addChild(this.infoText);
        //计时
        var jishi = createTextFiled("Time", 298, 25, 25, 0xffffff)
        this.addChild(jishi)
        var jishi2 = createTextFiled("00.00")
        jishi2.multiline = true;
        jishi2.size = 50;
        jishi2.textColor = 0xffffff;
        jishi2.y = 60;
        jishi2.x = 254;
        this.addChild(jishi2)
        var jishitime = 0;
        //游泳动画
        this.mcFactory = new egret.MovieClipDataFactory(RES.getRes("swin_json"), RES.getRes("swin_png"));
        this.mc = new egret.MovieClip(this.mcFactory.generateMovieClipData("ready"));
        this.addChild(this.mc)
        this.mc.gotoAndPlay(0, 1)
        var isChange = true
        this.mc.x = 40;
        this.mc.y = 200;
        this.mc.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            // isChange = !isChange;
            if (!isChange) {
                this.mc.movieClipData = this.mcFactory.generateMovieClipData("ready")
                this.mc.play(-1);
            } else {
                // e.preventDefault;
                this.mc.movieClipData = this.mcFactory.generateMovieClipData("swin");
                this.mc.play(-1);
            }
        }, this)

        //水波动画
        //ready go!
        var tree = createTextFiled("ready");
        tree.textColor = 0xfff434;
        tree.multiline = true;
        tree.size = 100;
        tree.scaleX = 1;
        tree.scaleY = 1;
        tree.anchorOffsetX = tree.width * .5;
        tree.anchorOffsetX = tree.height * .5;
        tree.x = 320;
        tree.y = 300;
        this.addChild(tree);
        // this.count1.play(0, 1);//播放音效
        egret.Tween.get(tree).to({ scaleX: 1.2, scaleY: 1.2 }, 500).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
            tree.text = "3";
            tree.scaleX = 1;
            tree.scaleY = 1;
            tree.x = 320
            tree.anchorOffsetX = tree.width * .5;
            tree.anchorOffsetY = tree.height * .5;
            this.count1.play(0, 1);//播放音效
            egret.Tween.get(tree).to({ scaleX: 1.2, scaleY: 1.2 }, 500).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                tree.text = "2";
                tree.scaleX = 1;
                tree.scaleY = 1;
                tree.x = 320;
                this.count1.play(0, 1);//播放音效
                tree.anchorOffsetX = tree.width * .5;
                tree.anchorOffsetY = tree.height * .5;
                egret.Tween.get(tree).to({ scaleX: 1.2, scaleY: 1.2 }, 500).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                    tree.text = "1";
                    tree.scaleX = 1;
                    tree.scaleY = 1;
                    tree.x = 320
                    tree.anchorOffsetX = tree.width * .5;
                    tree.anchorOffsetY = tree.height * .5;
                    this.count2.play(0, 1);//播放音效
                    egret.Tween.get(tree).to({ scaleX: 1.2, scaleY: 1.2 }, 500).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                        tree.text = "GO!";
                        tree.scaleX = 1;
                        tree.scaleY = 1;
                        tree.x = 320
                        tree.anchorOffsetX = tree.width * .5;
                        tree.anchorOffsetY = tree.height * .5;
                        egret.Tween.get(tree).to({ scaleX: 1.2, scaleY: 1.2 }, 500).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                            tree.visible = true;
                            this.mc.touchEnabled = true;
                            this.soundChannel = this.swin_huanhu.play();//欢呼声
                            //计时开始
                            this.jishi_time = setInterval(function () {
                                jishitime += 40
                                jishi2.text = Math.floor(jishitime / 1000).toString() + "." + jishitime % 1000 / 10;
                            }, 40)
                            //水波纹加速
                            this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
                        }, this);
                    }.bind(this))
                }.bind(this))
            }.bind(this))
        }.bind(this))

        //提示左右键移动
        var swin_tishi = createTextFiled("点击左右键，向前游动")
        swin_tishi.width = this.width;
        swin_tishi.height = 60;
        swin_tishi.textAlign = "center";
        swin_tishi.size = 31;
        swin_tishi.textColor = 0xffffff;
        swin_tishi.y = 1056;
        this.addChild(swin_tishi)
        //点击增加动画帧
        // swin_leftjian.touchEnabled = true
        this.mc.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.speed += 5;
            this.fps += 2;

            /*** 点击舞台的时候设置项目的帧频 ***/
            this.mc.frameRate = Number(this.fps);
            console.log(this.fps)
            /*** 本示例关键代码段结束 ***/
        }, this);

        //this.rankinglistUI容器
        this.rankinglist = diva(0x000000, 0, 0, 0, 640, 0, 640, 1136, 0, 1136, 0, 1136, 0, 0);           //添加容器
        var rankinglist_bg = createBitmap("swin_bg_png")
        this.rankinglist.addChild(rankinglist_bg);
        //波纹背景【在index容器中】
        var ripple1 = createBitmap("bg_ripple_png", 0, 0);
        var ripple2 = createBitmap("bg_ripple_png", 630, 0);
        this.rankinglist.addChild(ripple1);
        this.rankinglist.addChild(ripple2);
        egret.Tween.get(ripple1, { loop: true }).to({ x: -630 }, 30000);
        egret.Tween.get(ripple2, { loop: true }).to({ x: 0 }, 30000);

        //奖牌图片
        var RL_medal = createBitmap("medal_png", 218, -362);
        egret.Tween.get(RL_medal).to({ y: 0 }, 300);
        this.rankinglist.addChild(RL_medal);

        //全国排名文本
        var RL_myrankingText = createTextFiled("全国排名", 286, 206, 19, 0xff4c22);
        egret.Tween.get(RL_myrankingText).to({ alpha: 0 }, 0).to({ alpha: 0 }, 300).to({ alpha: 1 }, 300);
        RL_myrankingText.bold = true;
        this.rankinglist.addChild(RL_myrankingText);

        //全国我的排名序号
        var RL_myrankingNumber = createTextFiled("4956", 240, 225, 72, 0xff4c22, "center", 170, 60, "top", false, 0xffffff, true);
        egret.Tween.get(RL_myrankingNumber).to({ alpha: 0 }, 0).to({ alpha: 0 }, 300).to({ alpha: 1 }, 300);
        RL_myrankingNumber.bold = true;
        this.rankinglist.addChild(RL_myrankingNumber);

        //我的名字
        var RL_myname = createTextFiled("ChristyLouis", 65, 305, 32, 0xffffff, "center", 156, 65, "top", false, 0xffffff, true);
        RL_myname.bold = true;
        this.rankinglist.addChild(RL_myname);

        //我的Timer
        var RL_myTimer = createTextFiled("Timer", 478, 224, 18, 0xffffff);
        RL_myTimer.bold = true;
        this.rankinglist.addChild(RL_myTimer);

        //我的Time
        var RL_mytime = createTextFiled("00:00", 445, 247, 42, 0xffffff);
        RL_mytime.bold = true;
        this.rankinglist.addChild(RL_mytime);

        //椭圆框
        var top10 = new egret.Shape();
        top10.graphics.lineStyle(2, 0xffffff);
        top10.graphics.beginFill(0xffffff);
        top10.graphics.drawRoundRect(65, 384, 515, 89, 89, 89);
        top10.graphics.endFill();
        this.rankinglist.addChild(top10);

        // function createTextFiled(text = "", x = 0, y = 0, size = 30, textColor = 0x000000, textAlign = "center", width = 0, height = 0,verticalAlign = "middle", border = false, borderColor = 0x000000, multiline = false, type = egret.TextFieldType.DYNAMIC, ) {
        //全国前十排行榜
        var top10Text = createTextFiled("指尖自由泳全国前10排行榜", 65, 384, 38, 0xff4c22, "center", 515, 89, "middle");
        top10Text.bold = true;
        this.rankinglist.addChild(top10Text);

        //前往领奖台抽奖
        var RL_toDraw = createBitmap("toDraw_png", 260, 973);
        RL_toDraw.touchEnabled = true;
        RL_toDraw.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            RL_toDraw.touchEnabled = false;
            egret.Tween.get(RL_toDraw).to({ y: 980 }, 300).to({ y: 973 }, 200)
            SenceManage.create(this.stage).loadSence("preload", null, PersonUI);
            egret.Tween.get(this).to({ x: -640 }, 500);

        }, this)
        this.rankinglist.addChild(RL_toDraw);


        //抽奖排行容器
        // function div(bgclor=0x262626,alpha=0.5,topleft_x,topleft_y,topright_x,topright_y,lowerright_x,lowerright_y,lowerleft_x,lowerleft_y,end_x,end_y,div_x,div_y) {
        // var RL_content = div(0x000000, 0.5, 0, 0, 512, 487, 512, 888, 62, 888, 62, 487, 0, 0);
        // var RL_content = div(0x000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        var RL_content = new egret.DisplayObjectContainer();
        this.rankinglist.addChild(RL_content);

        //滚动视图scoll
        var RL_scrollView = new egret.ScrollView();
        RL_scrollView.horizontalScrollPolicy = "off";
        RL_scrollView.x = 95;
        RL_scrollView.y = 487;
        RL_scrollView.width = 450;
        RL_scrollView.height = 420;
        this.rankinglist.addChild(RL_scrollView);
        RL_scrollView.setContent(RL_content);//scollview设置在容器中


        this.addChild(this.rankinglist);
        this.rankinglist.visible = false;
        //名次函数封装开始 
        function rlListContent(contenty, imgbg, num, name, time, imgurl) {
            var RL_contentNum = diva(0x000000, 0, 0, 0, 450, 0, 450, 88, 0, 88, 0, 0, 0, contenty);//y
            RL_content.addChild(RL_contentNum);
            var RL_first = createBitmap(imgbg, 0, 0);//图片
            RL_contentNum.addChild(RL_first);
            var list_number1 = createTextFiled(num, 10, 0, 40, 0x000000, "center", 60, 87, "middle");//数字
            RL_contentNum.addChild(list_number1);

            RES.getResByUrl(imgurl, function (texture: egret.Texture): void {
                var result: egret.Bitmap = new egret.Bitmap();
                result.texture = texture;
                result.width = 70;
                result.height = 70;
                // result.x = 70;
                // result.y = 9;
                result.x = 175;
                result.y = 53;
                result.anchorOffsetX = 105;
                result.anchorOffsetY = 44;
                RL_contentNum.addChild(result);
                //列表头像的遮罩
                var RLptmask = new egret.Shape();
                RLptmask.graphics.beginFill(0x000000);
                RLptmask.graphics.drawCircle(105, 44, 36);
                RLptmask.graphics.endFill();
                RL_contentNum.addChild(RLptmask);
                result.mask = RLptmask;
            }, this, RES.ResourceItem.TYPE_IMAGE);

            var list_name1 = createTextFiled(name, 157, 0, 32, 0xff4c22, "center", 140, 88, "middle");//名字
            list_name1.bold = true;
            RL_contentNum.addChild(list_name1);
            var list_timeText1 = createTextFiled("Time", 378, 11, 18, 0xff4c22, "right");
            list_timeText1.bold = true;
            RL_contentNum.addChild(list_timeText1);
            var list_timeNumber1 = createTextFiled(time, 290, 35, 42, 0xff4c22, "right", 135, 87, "top");//时间成绩
            list_timeNumber1.bold = true;
            RL_contentNum.addChild(list_timeNumber1);
            return RL_contentNum;
        }
        //名次函数结束 

        // 名次请求
        MYajax({ "type": "ranking_list" }, "http://swin.bluej.cn/index.php/Home/app/ranking_list.html", function (res) {
            // console.log(res);
            for (var i = 0; i < res.data.length; i++) {
                var ibg = i == 0 ? "RLfirst_png" : i == 1 ? "RLsecond_png" : i == 2 ? "RLthird_png" : "RLnumber_png";
                RL_content.addChild(rlListContent(i * 107, ibg, i + 1, res.data[i].name, res.data[i].game_time, res.data[i].image));
            }
        });
        //名次请求结束

        //获取头像和名字开始
        MYajax({ "type": "userinfo" }, "http://swin.bluej.cn/index.php/Home/app/get_userinfo.html", function (res) {
            // console.log(res);
            RL_myname.text = res.data.name;
            RES.getResByUrl(res.data.image, function (texture: egret.Texture): void {
                var result: egret.Bitmap = new egret.Bitmap();
                result.texture = texture;
                result.x = 230;
                result.y = 464;
                result.anchorOffsetX = 142;
                result.anchorOffsetY = 276;
                result.width = 108;
                result.height = 108;
                this.rankinglist.addChild(result);
                //我的头像的遮罩
                var ptmask = new egret.Shape();
                ptmask.graphics.beginFill(0x000000);
                ptmask.graphics.drawCircle(142, 242, 54);
                ptmask.graphics.endFill();
                this.rankinglist.addChild(ptmask);
                result.mask = ptmask;
            }, this, RES.ResourceItem.TYPE_IMAGE);
        }.bind(this));
        //获取头像和名字结束

        // //获取成绩时间和全国我的排名
        MYajax({ "type": "me_ranking" }, "http://swin.bluej.cn/index.php/Home/app/me_ranking.html", function (res) {
            // console.log(res);
            RL_mytime.text = res.time;
            RL_myrankingNumber.text = res.data;
        });

        // //获取时间待操作结束
        //晒战绩分享
        // var RL_shouRecord = createBitmap("shouRecord_png", 65, 973);
        // RL_shouRecord.touchEnabled = true;
        // RL_shouRecord.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
        //     sharemask.visible = true;
        //     egret.Tween.get(showone).to({ x: 256, y: 37 }, 300);
        // }, this)
        // this.rankinglist.addChild(RL_shouRecord);

        //晒战绩分享
        var RL_shouRecord = createBitmap("shouRecord_png", 65, 973);
        RL_shouRecord.touchEnabled = true;
        RL_shouRecord.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            sharemask.visible = true;
            egret.Tween.get(showone).to({ x: 256, y: 37 }, 100);
            egret.Tween.get(RL_shouRecord).to({ y: 980 }, 300).to({ y: 973 }, 200);
        }, this)
        this.rankinglist.addChild(RL_shouRecord);

        //分享的遮罩
        var sharemask = diva(0x262626, 0.5, 0, 0, 640, 0, 640, 1136, 0, 1136, 0, 0, 0, 0);           //添加容器
        sharemask.visible = false;
        sharemask.touchEnabled = true;
        sharemask.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            sharemask.visible = false;
            egret.Tween.get(showone).to({ x: -124, y: 977 }, 200);
        }, this)
        this.rankinglist.addChild(sharemask);

        //分享文本
        var showone = createBitmap("showone_png", -124, 977);
        // var showone = createBitmap("showone_png", 224-348, 977);
        sharemask.addChild(showone);



    }
    //鼠标不再点击时。逐渐降低速度。类似于惯性效果
    private time_S = setInterval(function () {
        this.speed -= 2;
        this.fps -= 1;
        if (this.fps <= 16) {
            this.fps = 16
        }
        // console.log(this.speed)
        if (this.speed <= 0) {
            this.speed = 0;
            this.mc.stop();
            // clearInterval(time_S)
        }
    }.bind(this), 100)
    private loop() {
        var self = this;
        // egretBird.rotation += 3;
        this.swin_shuihuaBG.y += this.speed;
        this.swin_shuihuaBG2.y += this.speed;
        this.swin_shuihuaBG2_1.y += this.speed;
        this.swin_shuihuaBG3.y += this.speed;
        //     egret.Tween.get(this.swin_shuihuaBG2, { loop: true }).to({ y:this.swin_shuihuaBG2.y+= this.speed},1000)
        //    egret.Tween.get(this.swin_shuihuaBG2_1, { loop: true }).to({ y:this.swin_shuihuaBG2_1.y+= this.speed},1000)
        // 到达终点
        if (this.swin_shuihuaBG3.y >= 0) {

            this.zhongdian.y += this.speed
        }
        if (this.swin_shuihuaBG3.y >= 280) {
            clearInterval(this.jishi_time);
            this.mc.touchEnabled = false;
            this.speed = 0;
            this.infoText.text = "WIN!"
            this.soundChannel.stop();
            this.infoText.x = 370;
            this.infoText.y = 300;
            self.addChild(this.infoText);
            this.mc.movieClipData = this.mcFactory.generateMovieClipData("ready");
            this.mc.stop();
            egret.Tween.get(this.infoText).to({ scaleX: 1, scaleY: 1 }, 500).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                this.rankinglist.visible = true;

            }.bind(this))
        }
    }
    public setProgress(current: number, total: number): void {
        this.textField.text = `Loading...${current}/${total}`;
    }

}